//
//  RegistNextViewController.m
//  FeiYang
//
//  Created by user on 15/10/14.
//  Copyright © 2015年 feiyang. All rights reserved.
//

#import "RegistNextViewController.h"
#import "LoginViewController.h"
@interface RegistNextViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

@property(nonatomic,strong)NSArray      *placeHolderArr;
@property(nonatomic,strong)UITableView  *registTab;
@property(nonatomic,strong)UIButton    *registBtn;
@end

@implementation RegistNextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTheNavigationBar];
    self.view.backgroundColor = [UIColor whiteColor];
    
    if (!_placeHolderArr) {
//        _placeHolderArr = @[@"请输入工号",@"请输入密码",@"请再次输入密码"];
        _placeHolderArr = @[@"请输入密码",@"请再次输入密码"];

    }
    [self createUI];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardShow:)
                                                 name: UIKeyboardDidShowNotification object:nil];
    //注册键盘隐藏通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardHide:)
                                                 name: UIKeyboardDidHideNotification object:nil];
    
}

#pragma mark -------------创建导航栏及相关控件
-(void)createTheNavigationBar
{
    
    //    自定义navgationBar
    TopView *topView = [[TopView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, SCREEN_WIDTH, 64.0f)];
    [topView setTitile:@"注册"];
    [self.view addSubview:topView];
    
    
    UIButton * backBtn = [[UIButton alloc]initWithFrame:CGRectMake(10.0f, 35.0f, 49.0f, 16.0f)];
    [backBtn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnCLick:) forControlEvents:UIControlEventTouchUpInside];
    backBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    [topView addSubview:backBtn];
}
- (void)backBtnCLick:(UIButton*)btn
{
    [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"" withType:TRUE];
    [self dismissViewControllerAnimated:TRUE completion:^(void){}];
}

-(void)createUI
{
    //    分割线
    
    UILabel *sepLab1 = [[UILabel alloc] initWithFrame:CGRectMake(8.0f, 16.0f+50.0f, SCREEN_WIDTH-16.0f, .5f)];
    sepLab1.backgroundColor = [UIColor hexStringToColor:kColorGraySperator];
    
//    UILabel *sepLab2 = [[UILabel alloc] initWithFrame:CGRectMake(8.0f, 16.0f+50.0f+50.0f, SCREEN_WIDTH-16.0f, .5f)];
//    sepLab2.backgroundColor = kColorGraySperator;
    
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardhide)];
    tapGesture.cancelsTouchesInView = NO;
    [self.registTab addGestureRecognizer:tapGesture];
    
    [self.registTab addSubview:sepLab1];
//    [self.registTab addSubview:sepLab2];
    
    [self.view addSubview:self.registTab];
    [self.registTab addSubview:self.registBtn];
}
-(UITableView *)registTab
{
    if (!_registTab) {
        _registTab = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 64.0f, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
        _registTab.delegate = self;
        _registTab.dataSource = self;
        _registTab.rowHeight = 50.0f;
        _registTab.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    }
    return _registTab;
}

//注册按钮
-(UIButton *)registBtn
{
    if (!_registBtn) {
        _registBtn = [[UIButton alloc] initWithFrame:CGRectMake(10.0f,182.0f, SCREEN_WIDTH- 20.0f, 44.0f)];
        [_registBtn addTarget:self action:@selector(buttonNextTapped) forControlEvents:UIControlEventTouchUpInside];
        [_registBtn setTitle:@"完成注册" forState:UIControlStateNormal];
        [_registBtn setBackgroundColor:[UIColor hexStringToColor:COLOR_VALUE] forState:UIControlStateNormal];
        _registBtn.layer.cornerRadius = 2;
        _registBtn.layer.masksToBounds = YES;
        
    }
    return _registBtn;
}

#pragma mark-tableViewDelegate&datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.placeHolderArr.count;
}
static UITextField *__textField;//记录当前工号的textField
static UITextField *__textFieldPwd;//记录当前密码的textField
static UITextField *__textFieldConfirmPwd;//记录当前确认密码的textField
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
        
        UITextField *userTF = [[UITextField alloc] init];
        userTF.tag = 2000;
        userTF.frame = CGRectMake(16.0f, 5.0f, SCREEN_WIDTH-32.0f, 45.0f);
        userTF.delegate = self;
        userTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        
        [userTF setValue:[UIFont boldSystemFontOfSize:14.0f] forKeyPath:@"_placeholderLabel.font"];
        userTF.font = [UIFont systemFontOfSize:14.0f];
        [cell.contentView addSubview:userTF];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    UITextField *text = (UITextField *)[cell.contentView viewWithTag:2000];
    text.placeholder = [_placeHolderArr objectAtIndex:indexPath.row];
    
    
//    if (indexPath.row==0) {
//        
//        __textField = text;
//        __textField.keyboardType = UIKeyboardTypeNumberPad;
//    }
//    if (indexPath.row==1) {
//        __textFieldPwd = text;
//        text.secureTextEntry = YES;
//        
//        
//    }
//    if (indexPath.row==2) {
//        
//        __textFieldConfirmPwd = text;
//        text.secureTextEntry = YES;
//        
//    }
    
    switch (indexPath.row) {
        case 0:
        {
            __textFieldPwd = text;
            text.secureTextEntry = YES;
        }
            break;
        case 1:
        {
            __textFieldConfirmPwd = text;
            
            text.secureTextEntry = YES;
        }

            break;
        default:
            break;
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 16.0f;
}


- (void)buttonNextTapped
{
//    if ([AccountTools isEmpty:__textField.text]==YES) {
//        ALERT_VIEW(@"请输入工号")
//    }
    
   if ([AccountTools isEmpty:__textFieldPwd.text]==YES) {
        ALERT_VIEW(@"请输入密码")
    }
    
    else if ([AccountTools isEmpty:__textFieldConfirmPwd.text]==YES) {
        ALERT_VIEW(@"请输入确认密码")
    }
    
//    else if (__textField.text.length!=8) {
//        ALERT_VIEW(@"工号输入必须为8位数字")
//    }
    else if ([__textFieldConfirmPwd.text isEqualToString:__textFieldPwd.text])
    {
        
        if ([AccountTools validatePassWord:__textFieldPwd.text]==NO) {
            ALERT_VIEW(@"密码必须为8~32位字符")
        }else{
            
            //执行
            [[AnimationIndicator sharedIndicator] startAnimation];
            [AppGlobalClass PostDataWithNSstring:REGISTER withDictionary:@{@"userCd":self.mobile,@"password":__textFieldPwd.text,@"checkCode":self.checkCode, @"userType":[NSString stringWithFormat:@"0"]} success:^(NSDictionary *dictionary) {
                if([[dictionary objectForKey:@"success"] isEqualToNumber:[NSNumber numberWithBool:TRUE]])
                {
                    //注册成功回到登录界面
                    ALERT_VIEW(@"注册成功")
                    [self.presentingViewController.presentingViewController dismissViewControllerAnimated:TRUE completion:^(void){}];
                }else
                {
                    NSString * str = [dictionary objectForKey:@"message"];
                    ALERT_VIEW(str);
                }
                [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"完成" withType:TRUE];
            } failure:^(NSError *error) {
                [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"失败" withType:TRUE];
            } networkerror:^(NSString *networkerror) {
                [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"无网络" withType:TRUE];
            }];
        }
        
    }
    else{
        ALERT_VIEW(@"两次密码输入不一致");
    }
    
    
    
}

- (void)keyboardhide
{
    [__textFieldPwd resignFirstResponder];
//    [__textField resignFirstResponder];
    [__textFieldConfirmPwd resignFirstResponder];
    
}



-(void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    //解除键盘出现通知
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name: UIKeyboardDidShowNotification object:nil];
    //解除键盘隐藏通知
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name: UIKeyboardDidHideNotification object:nil];
}

//键盘下降
- (void)keyboardHide:(NSNotification *)noti
{
    
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:[[noti.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
    [UIView setAnimationCurve:7];
    _registTab.contentOffset = CGPointMake(0, 0);
    
    [UIView commitAnimations];
}

//键盘升起
- (void)keyboardShow:(NSNotification *)noti
{
    NSValue *value = [noti.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardFrame = [value CGRectValue];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:[[noti.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
    [UIView setAnimationCurve:7];
    if (keyboardFrame.origin.y != SCREEN_HEIGHT) {
        CGFloat a = _registBtn.frame.origin.y+_registBtn.frame.size.height+_registTab.frame.origin.y+80.0f;
        if (a < keyboardFrame.origin.y) {
            _registTab.contentOffset = CGPointMake(0, 0);
        }else
        {
            _registTab.contentOffset = CGPointMake(0, 50);
        }
        
    }
    
    [UIView commitAnimations];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
